﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseHelper.Base
{
    public class TimeHelper
    {

        public static long ToTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }

        public static long ToTimeStamp(DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(time - startTime).TotalSeconds; // 相差秒数
            return timeStamp;
        }


        public static DateTime ToDateTime(long timeStamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddSeconds(timeStamp);
        }

        public static DateTime ToDateTime(string timeStamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddSeconds(Convert.ToInt64(timeStamp));
        }

        public static string GetTimeSpan(TimeSpan ts)
        {
            string timespan = "";
            if (ts.Days > 0)
                timespan = ts.Days.ToString() + "天";
            if (ts.Hours > 0)
                timespan += ts.Hours.ToString() + "小时";
            timespan += ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒";

            return timespan;
        }

        public static string GetTimeSpan(int second)
        {
            return GetTimeSpan(new TimeSpan(0, 0, second));
        }
    }
}
